import axios from "@/libs/api.request";
import { GD_MAP_KEY, GD_POSITION_URI, GD_GPS_URI } from "./config";
export const getPostion = (str) => {
  let url =
    GD_POSITION_URI +
    resoleParams({
      key: GD_MAP_KEY,
      location: str
    });
  return axios.request({
    url,
    method: "get"
  });
};

export const resoleParams = params => {
  let objs = [];
  for (let key in params) {
    if (params[key]) {
      objs.push([key, encodeURIComponent(params[key])].join("="));
    }
  }
  return objs.join("&");
};

export const gpsToGd = (lng, lat) => {
  let url =
    GD_GPS_URI +
    resoleParams({
      key: GD_MAP_KEY,
      locations: lng + "," + lat,
      coordsys: "gps"
    });
  return axios.request({
    url,
    method: "get"
  });
};
